﻿using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using LetsDoIt.Common.Constants;
using LetsDoIt.Logging;
using LetsDoItWS.BO;
using Microsoft.ServiceModel.Web;

[assembly: ContractNamespace("", ClrNamespace = "")]
namespace LetsDoItWS
{
	// TODO: Modify the service behavior settings (instancing, concurrency etc) based on the service's requirements. Use ConcurrencyMode.Multiple if your service implementation is thread-safe.
	// TODO: NOTE: Please set IncludeExceptionDetailInFaults to false in production environments
	[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
	[ServiceContract(Namespace = "")]
	public class AuthService
	{
		//setup version
		private const string version = Versions.Version_1_0;


		[OperationContract]
		[WebGet(ResponseFormat = WebMessageFormat.Xml, 
			BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = version + "/validate/{user}({passwordMD5})")]
		[WebHelp(Comment = "Validate user")]
        public bool ValidateUser(string user, string passwordMD5)
		{
		    int count = ValidateUserGeneric(user, passwordMD5);
		    return count > 0;
		}


	    [OperationContract]
		[WebGet(ResponseFormat = WebMessageFormat.Json,
			BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = version + "/validate/{user}({passwordMD5})/json")]
		[WebHelp(Comment = "Validate user")]
        public bool ValidateUserJson(string user, string passwordMD5)
		{
            int count = ValidateUserGeneric(user, passwordMD5);
            return count > 0;
		}


        private int ValidateUserGeneric(string user, string passwordMD5)
        {
            WebOperationContext ctx = WebOperationContext.Current;
            int count = 0;
            try
            {
                count = AuthenticationFactory.ValidateUser(user, passwordMD5);
                if (ctx != null)
                {
                    ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
                }
            }
            catch (Exception ex)
            {
                LoggerManager.Current.ErrorFormat(typeof(object), "WS Error : {0}", ex);
                if (ctx != null)
                {
                    ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
                    ctx.OutgoingResponse.StatusDescription = ex.ToString();
                }
            }
            return count;
        }


        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = version + "/usersalt/{user}")]
        [WebHelp(Comment = "Get user salt for hashing password (MD5)")]
        public string GetUserSalt(string user)
        {
            string salt = GetUserSaltGeneric(user);
            return salt;
        }

        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = version + "/usersalt/{user}/json")]
        [WebHelp(Comment = "Get user salt for hashing password (MD5)")]
        public string GetUserSaltJson(string user)
        {
            string salt = GetUserSaltGeneric(user);
            return salt;
        }

	    private string GetUserSaltGeneric(string user)
	    {
            WebOperationContext ctx = WebOperationContext.Current;
            string salt = string.Empty;
            try
            {
                salt = AuthenticationFactory.GetUserSalt(user);
                if (ctx != null)
                {
                    ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
                }
            }
            catch (Exception ex)
            {
                LoggerManager.Current.ErrorFormat(typeof(object), "WS Error : {0}", ex);
                if (ctx != null)
                {
                    ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
                    ctx.OutgoingResponse.StatusDescription = ex.ToString();
                }
            }
            return salt;
	    }


	    [OperationContract]
		[WebGet(ResponseFormat = WebMessageFormat.Xml,
			BodyStyle = WebMessageBodyStyle.Bare,
			UriTemplate = version + "/install")]
		[WebHelp(Comment = "Gets an app installation ID")]
		public string GetInstallationId()
		{
			return Guid.NewGuid().ToString();
		}

		[OperationContract]
		[WebGet(ResponseFormat = WebMessageFormat.Json,
			BodyStyle = WebMessageBodyStyle.Bare,
			UriTemplate = version + "/install/json")]
		[WebHelp(Comment = "Gets an app installation ID")]
		public string GetInstallationIdJson()
		{
			return Guid.NewGuid().ToString();
		}

	}
}
